﻿$(document).ready(function(){
	getSource();
	initEvent();
});
/**
 * 加载数据
 */
var myPagination;
function initEvent(){
	  $("#submitbutton").click(function(){
		   var formData = $("#queryForm").serialize(); //序列化表单
		   formData = decodeURIComponent(formData, true); //解码
		   $.fn.debug("开始指定加载");
		   myPagination.onLoad({param:formData});
		   $.fn.debug("结束指定加载");
	});
}
function getSource(){
	//var formData = $("#queryForm").formSerialize(); //序列化表单
    //formData = decodeURIComponent(formData, true);	//解码
	myPagination = $("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,// 
		cssStyle:'grayr',
		limit : 5,//每页显示条数
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "stbUserList",
		  //param:formData,
		  dataType: 'json',
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	}); 
}
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html = "";
	if(data.total >0){
		$.each(data.source,function(i,stb){
			html +='<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)" ondblclick="editAjax('+stb.id+')">';
			html +='<td style="padding-left: 5px; width:30px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+stb.id+'"/></td>';
			html +='<td width="80">'+stb.fstbid+'</td>';
			html +='<td width="100">'+stb.fstbcanumber+'</td>';
			html +='<td style="word-break : break-all;">'+stb.fuseragent+'</td>';
			html +='<td width="120">'+stb.faccesstime+'</td>';
			html +='<td width="200" style="word-break : break-all;">'+stb.fcurrentpage+'</td>';
			html +='<td width="70">'+stb.fregionid+'</td>';
			html +='<td width="100">'+stb.fip+'</td></tr>';
		});
	}else{
		html = "<tr><td colspan='8' align='center'>暂无数据</td></tr>";
	}
	$("#stbs").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
		
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = [];
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	}); 
	return ids;  
}
/**
 * 删除站点信息
 */
function deleteStb(){
	var ids = checkedInfo();
	$("input[name=weball]").attr("checked",false);
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s};
		$.ajax({
			url:"deleteStb",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功删除数据'+data+'条');
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据删除失败'); 
			}
		
		});
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
